package com.blockchain.console.service.system.achieve;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.blockchain.console.service.system.ConstantManage;
import com.blockchain.console.service.system.entity.Constant;
import com.blockchain.console.service.system.query.ConstantLogQuery;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.query.ArrayParser;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.StringHelper;

/**
 * @author blockchain
 * 
 */
public class ConstantManageImpl extends AbstractSystemService implements
		ConstantManage {

	public ConstantManageImpl(ServiceResource serviceResource) {
		super(serviceResource);
	}

	public static class EmailManageFactory implements
			ServiceFactory<ConstantManage> {

		@Override
		public ConstantManage newInstance(ServiceResource serviceResource) {
			return new ConstantManageImpl(serviceResource);
		}
	}

	@Override
	public void addConstantLog(String key, String name, String value1,
			String value2) throws Throwable {
		String sql = "INSERT INTO T7102 SET F02=?,F03=?,F04=?,F05=?,F06=?,F07=?";
		execute(getConnection(), sql, key, name, value1, value2,
				serviceResource.getSession().getAccountId(), new Timestamp(
						System.currentTimeMillis()));
	}

	@Override
	public PagingResult<Constant> search(ConstantLogQuery query, Paging paging)
			throws Throwable {
		StringBuilder sb = new StringBuilder(
				"SELECT T7102.F01,T7102.F02,T7102.F03,T7102.F04,T7102.F05,T7011.F02 AS NAME,T7102.F07 FROM T7102 INNER JOIN T7011 ON T7102.F06=T7011.F01 WHERE 1=1");
		List<Object> parameters = new ArrayList<>();
		if (query != null) {
			String key = query.getKey();
			if (!StringHelper.isEmpty(key)) {
				sb.append(" AND T7102.F02 LIKE ?");
				parameters.add(getSQLConnectionProvider().allMatch(key));
			}
			String name = query.getName();
			if (!StringHelper.isEmpty(name)) {
				sb.append(" AND T7102.F03 LIKE ?");
				parameters.add(getSQLConnectionProvider().allMatch(name));
			}
			Timestamp timeStart = query.getTimeStart();
			Timestamp timeEnd = query.getTimeEnd();
			if (timeStart != null) {
				sb.append(" AND DATE(T7102.F07)>=?");
				parameters.add(timeStart);
			}
			if (timeEnd != null) {
				sb.append(" AND DATE(T7102.F07)<=?");
				parameters.add(timeEnd);
			}
		}
		return selectPaging(getConnection(), new ArrayParser<Constant>() {

			public Constant[] parse(ResultSet resultSet) throws SQLException {
				List<Constant> lists = new ArrayList<>();
				while (resultSet.next()) {
					Constant constant = new Constant();
					constant.id = resultSet.getInt(1);
					constant.key = resultSet.getString(2);
					constant.desc = resultSet.getString(3);
					constant.value1 = resultSet.getString(4);
					constant.value2 = resultSet.getString(5);
					constant.name = resultSet.getString(6);
					constant.updateTime = resultSet.getTimestamp(7);
					lists.add(constant);
				}
				return lists.toArray(new Constant[lists.size()]);
			}
		}, paging, sb.toString(), parameters);
	}
}
